package com.xiaoyu.lanling.feature.moment.viewholder.detail

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.emoji.widget.EmojiTextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.moment.detail.TryDeleteCommentEvent
import com.xiaoyu.lanling.event.moment.detail.TryReplyCommentEvent
import com.xiaoyu.lanling.feature.moment.model.detail.CommentItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

class CommentItemViewHolder : ViewHolderBase<CommentItem>() {
    private lateinit var rootView: View
    private lateinit var commentIcon: ImageView
    private lateinit var name: UserNameTextView
    private lateinit var avatar: UserAvatarDraweeView
    private lateinit var comment: EmojiTextView
    private lateinit var time: TextView
    var pendingDeleteCommentItem: CommentItem? = null

    private val onAvatarClickListener = object : OnClickDebounceListener() {

        override fun onClickDebounce(v: View) {
            val itemData = v.getViewTag<CommentItem>() ?: return
            val user = itemData.user
            val activity = App.getInstance().topActivity ?: return

            Router.instance.gotoUserActivity(activity, user)
        }
    }

    private val onItemClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val itemData = v.getViewTag<CommentItem>() ?: return
            val user = itemData.user
            if (UserData.getInstance().isSelf(user.uid)) {
                return
            }
            TryReplyCommentEvent(itemData).post()
        }
    }

    private val onItemLongClickListener = object : View.OnLongClickListener {
        override fun onLongClick(v: View): Boolean {
            val activity = App.getInstance().topActivity ?: return false
            val comment = v.getViewTag<CommentItem?>() ?: return false
            if (UserData.getInstance().isSelf(comment.user.uid).not()) {
                return false
            }
            pendingDeleteCommentItem = comment

            AlertDialog.Builder(activity)
                    .setItems(arrayOf("删除")) { dialog, which ->
                        dialog.dismiss()
                        showCommentDeleteRationale()
                    }
                    .show()

            return true
        }
    }

    private fun showCommentDeleteRationale() {
        val activity = App.getInstance().topActivity ?: return
        AlertDialog.Builder(activity)
                .setTitle(AppContext.getString(R.string.action_delete))
                .setMessage(AppContext.getString(R.string.moment_delete_comment_rationale))
                .setNegativeButton(R.string.cancel) { dialog, which ->
                    pendingDeleteCommentItem = null
                    dialog.dismiss()
                }
                .setPositiveButton(R.string.action_confirm) { dialog, which ->
                    pendingDeleteCommentItem?.let {
                        TryDeleteCommentEvent(Any(), JsonData.newMap(), it.comment.id).post()
                        dialog.dismiss()
                    }
                }
                .setOnDismissListener {
                    pendingDeleteCommentItem = null
                }
                .create()
                .show()
    }

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        rootView = layoutInflater.inflate(R.layout.moment_detail_comment_item, parent, false)
        commentIcon = rootView.findViewById(R.id.commentIcon)
        name = rootView.findViewById(R.id.name)
        avatar = rootView.findViewById(R.id.avatar)
        comment = rootView.findViewById(R.id.comment)
        time = rootView.findViewById(R.id.time)
        avatar.setOnClickListener(onAvatarClickListener)
        rootView.setOnClickListener(onItemClickListener)
        rootView.setOnLongClickListener(onItemLongClickListener)
        return rootView
    }

    override fun showData(position: Int, itemData: CommentItem) {
        commentIcon.visibility = if (position == 0) View.VISIBLE else View.GONE
        name.text = itemData.user.name
        ImageViewUtil.loadImage(avatar, itemData.avatarLoadParam)
        comment.text = itemData.comment.content
        time.text = itemData.comment.time
        avatar.setViewTag(itemData)
        rootView.setViewTag(itemData)
    }
}